package com.carAnalysisAdmin.common.utils;

import java.util.*;

public class ElevatorystemUtil {

    static class Edge {
        int destination;
        int weight;

        public Edge(int destination, int weight) {
            this.destination = destination;
            this.weight = weight;
        }
    }

    public static int[] dijkstra(List<List<Edge>> graph, int source) {
        int[] dist = new int[graph.size()];
        Arrays.fill(dist, Integer.MAX_VALUE);
        dist[source] = 0;

        PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.comparingInt(u -> dist[u]));
        pq.offer(source);

        while (!pq.isEmpty()) {
            int u = pq.poll();
            for (Edge e : graph.get(u)) {
                int v = e.destination;
                int alt = dist[u] + e.weight;
                if (alt < dist[v]) {
                    dist[v] = alt;
                    pq.offer(v);
                }
            }
        }
        return dist;
    }

    public static int calculateShortestTime(int sourceFloor, int destinationFloor, int numFloors) {
        List<List<Edge>> graph = new ArrayList<>();
        for (int i = 0; i < numFloors; i++) {
            graph.add(new ArrayList<>());
        }

        for (int i = 0; i < numFloors - 1; i++) {
            graph.get(i).add(new Edge(i + 1, 2));
            graph.get(i + 1).add(new Edge(i, 2));
        }

        int[] shortestTimes = dijkstra(graph, sourceFloor - 1);
        return shortestTimes[destinationFloor - 1] * 2;
    }

    public static int getMaxTime(int[] sourceFloors, int[] destinationFloors, int numFloors) {
        int maxTime = Integer.MIN_VALUE;
        for (int i = 0; i < sourceFloors.length; i++) {
            int time = calculateShortestTime(sourceFloors[i], destinationFloors[i], numFloors);
            maxTime = Math.max(maxTime, time);
        }
        return maxTime;
    }

    public static void main(String[] args) {
        int[] sourceFloors = {2};
        int[] destinationFloors = {3};
        int numFloors = 5;
        int maxTime = Integer.MIN_VALUE;

        for (int i = 0; i < sourceFloors.length; i++) {
            int time = calculateShortestTime(sourceFloors[i], destinationFloors[i], numFloors);
            maxTime = Math.max(maxTime, time);
        }

        System.out.println("Maximum time for elevator travel: " + maxTime + " minutes");
    }
}
